package de.jonasjelten.jcalc.gui;

import java.io.*;
import javax.swing.JFileChooser;

/**
 *
 * @author fabian
 */
class txtFilter extends javax.swing.filechooser.FileFilter {

    @Override
    public boolean accept(File file) {
        return file.isDirectory() || file.getAbsolutePath().endsWith(".txt");
    }

    @Override
    public String getDescription() {
        return "Text documents (*.txt)";
    }
}

public class FileChooserWindow extends javax.swing.JFrame {

    /** Creates new form FileChooserWindow */
    public MainWindow ownerWindow;

    public FileChooserWindow(MainWindow ownerWindow) {
        super();
        this.ownerWindow = ownerWindow;
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        SaveTxtFileChooser = new javax.swing.JFileChooser();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Save jCalc calculation as txt file");

        SaveTxtFileChooser.setDialogTitle("Berechnung als .txt speichern");
        SaveTxtFileChooser.setDialogType(javax.swing.JFileChooser.SAVE_DIALOG);
        SaveTxtFileChooser.setFileFilter(new txtFilter());
        SaveTxtFileChooser.setPreferredSize(new java.awt.Dimension(600, 400));
        SaveTxtFileChooser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SaveFile(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(SaveTxtFileChooser, javax.swing.GroupLayout.DEFAULT_SIZE, 604, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(SaveTxtFileChooser, javax.swing.GroupLayout.DEFAULT_SIZE, 408, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void SaveFile(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveFile
        SaveAction(evt.getActionCommand());

    }//GEN-LAST:event_SaveFile
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFileChooser SaveTxtFileChooser;
    // End of variables declaration//GEN-END:variables

    private void SaveAction(String command) {
        if (command.equals(JFileChooser.CANCEL_SELECTION)) {
            this.dispose();
        } else if (command.equals(JFileChooser.APPROVE_SELECTION)) {
            String filename = SaveTxtFileChooser.getSelectedFile().toString();
            if (!filename.substring(filename.length() - 3).equals("txt")) {
                filename = filename + ".txt";
            }
            String text = ownerWindow.ReturnSummarizedCalc();
            if (SaveToTxt(filename, text)) {
                this.dispose();
            } else {
                System.out.println("An error occured saving the file");
            }
        }
    }

    public boolean SaveToTxt(String filename, String text) {
        try {
            FileWriter outFile = new FileWriter(filename);
            PrintWriter out = new PrintWriter(outFile);
            out.println(text);
            out.close();
            return true;
        } catch (IOException e) {
            System.out.println(e);
            return false;
        }
    }
}
